package com.ruoyi.operator.controller;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.operator.domain.OptShop;
import com.ruoyi.operator.service.IOptShopService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户信息Controller
 *
 * @author ruoyi
 * @date 2021-01-22
 */
@Controller
@RequestMapping("/operator/shop")
public class OptShopController extends BaseController {

    private String prefix = "operator/shop";

    @Autowired
    private IOptShopService optShopService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private ISysRoleService roleService;

    @RequiresPermissions("operator:shop:view")
    @GetMapping()
    public String shop() {
        return prefix + "/shop";
    }

    /**
     * 查询商户信息列表
     */
    @RequiresPermissions("operator:shop:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OptShop optShop) {
        startPage();
        SysUser currentUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        if (!(currentUser.getDeptId() == 100)) { optShop.setDeptId(currentUser.getDeptId()); }
        List<OptShop> list = optShopService.selectOptShopList(optShop);
        return getDataTable(list);
    }

    /**
     * 导出商户信息列表
     */
    @RequiresPermissions("operator:shop:export")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OptShop optShop) {
        List<OptShop> list = optShopService.selectOptShopList(optShop);
        ExcelUtil<OptShop> util = new ExcelUtil<OptShop>(OptShop.class);
        return util.exportExcel(list, "shop");
    }


    /**
     * 新增商户信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商户信息
     */
    @RequiresPermissions("operator:shop:add")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OptShop optShop) {
        return toAjax(optShopService.insertOptShop(optShop));
    }

    /**
     * 修改商户信息
     */
    @GetMapping("/edit/{shopId}")
    public String edit(@PathVariable("shopId") String shopId, ModelMap mmap) {
        OptShop optShop = optShopService.selectOptShopByShopId(shopId);
        mmap.put("optShop", optShop);
        return prefix + "/edit";
    }

    /**
     * 修改保存商户信息
     */
    @RequiresPermissions("operator:shop:edit")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OptShop optShop) {
        return toAjax(optShopService.updateOptShop(optShop));
    }

    /**
     * 删除商户信息
     */
    @RequiresPermissions("operator:shop:remove")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String shopIds) {

        return toAjax(optShopService.deleteOptShopByShopIds(shopIds));
    }


    /**
     * 审核商铺
     */
    @GetMapping("/examine/{shopId}")
    public String examine(@PathVariable("shopId") String shopId, ModelMap mmap) {
        OptShop optShop = optShopService.selectOptShopByShopId(shopId);
        mmap.put("optShop", optShop);
        return prefix + "/examine";
    }

    /**
     * 审核保存商户信息
     */
    @RequiresPermissions("operator:shop:examine")
    @Log(title = "商户审核", businessType = BusinessType.EXAMINE)
    @PostMapping("/examine")
    @ResponseBody
    public AjaxResult examineSave(OptShop optShop) {
        if (StringUtils.isNull(optShop.getDeptId())) {
            return  error("请先关联该商户的组织机构!");
        };
        return toAjax(optShopService.updateOptShop(optShop));
    }


    @RequiresPermissions("operator:shop:synchronous")
    @Log(title = "同步商家用户", businessType = BusinessType.EXAMINE)
    @PostMapping("/synchronous")
    @ResponseBody
    public AjaxResult synchronous(OptShop optShop) {
        OptShop optShopuser = optShopService.selectOptShopByShopId(optShop.getShopId());
        if (StringUtils.isNull(optShopuser.getDeptId())) {
            return error("请先关联该商户的组织机构!");
        }
        SysUser user = new SysUser();
        SysUser userinfo = null;
        user.setLoginName(optShop.getShopId());
        user.setPassword(configService.selectConfigByKey("sys.user.initPassword"));
        user.setPhonenumber(optShopuser.getTelnumber());
        user.setUserName(optShopuser.getLegalPerson());
        user.setDeptId(optShopuser.getDeptId());
        user.setSex("0");
        user.setUserType("01");
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(optShopuser.getShopId()))) {
            return error("该商户已同步!");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), optShopuser.getShoppwd(), user.getSalt()));
        user.setCreateBy(ShiroUtils.getLoginName());

        userService.insertUser(user);
        user.setPhonenumber(optShopuser.getTelnumber());
        userinfo = userService.selectdeptId(user);
        user.setUserId(userinfo.getUserId());
        user.setRoleId(Long.parseLong("2"));
        user.setRoleIds(new Long[]{Long.parseLong("2")});
        return toAjax(userService.updateUser(user));
    }


}


